import os

# 设置训练和验证标签文件夹路径
train_dir = r'F:\Frames\plastic_bottle\labels\train'
val_dir = r'F:\Frames\plastic_bottle\labels\val'

# 获取所有标签文件路径
def update_labels(directory):
    # 遍历目录中的所有标签文件
    for label_file in os.listdir(directory):
        if label_file.endswith('.txt'):  # 确保是 .txt 文件
            label_path = os.path.join(directory, label_file)

            # 读取标签文件
            with open(label_path, 'r') as f:
                lines = f.readlines()

            # 修改类标签：将 '1' 替换为 '0'
            with open(label_path, 'w') as f:
                for line in lines:
                    # 将行中的类标签 '1' 替换为 '0'
                    updated_line = line.replace('1 ', '0 ')  # 只替换类标签 1 为 0
                    f.write(updated_line)
            print(f"已更新文件: {label_file}")

# 修改训练集和验证集中的标签文件
update_labels(train_dir)
update_labels(val_dir)

print("标签更新完成！")
